package com.wnhoo.smartfishtank.base;

import android.annotation.TargetApi;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wnhoo.smartfishtank.R;

import java.util.Random;

import cn.jpush.android.api.JPushInterface;


public class BaseActivity extends FragmentActivity implements View.OnClickListener {

    public final String TAG = "BaseActivity";
    private Resources resources;

    public interface InitHeaderListener {

        void OnSetTitle(TextView t);

        void OnSetLeftView(Button btn);

        void OnSetRightView(Button btn);

        void OnSetLeftOnclick(View view);

        void OnSetRightOnclick(View view);

        void onSetbackground(View view);

    }

    private InitHeaderListener initHeaderListener;
    public View header_include;
    public TextView header_title;
    public Button header_btn_left, header_btn_right;
    public PopupWindow mpopupWindow;
    private boolean isSetBG = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * @param isSetDefaultBG
     */
    public void initView(boolean isSetDefaultBG) {

        this.isSetBG = isSetDefaultBG;
        this.initView();
    }

    public void initView() {
        header_include = findViewById(R.id.header_include);
        header_title = (TextView) findViewById(R.id.header_title);
        header_btn_left = (Button) findViewById(R.id.header_btn_left);
        header_btn_right = (Button) findViewById(R.id.header_btn_right);
        if (this.initHeaderListener != null) {
            this.initHeaderListener.OnSetTitle(header_title);
            header_btn_left.setOnClickListener(this);
            header_btn_right.setOnClickListener(this);
            this.initHeaderListener.OnSetLeftView(header_btn_left);
            this.initHeaderListener.OnSetRightView(header_btn_right);
            this.initHeaderListener.onSetbackground(header_include);
        }

    }

    public void setOnInitListener(InitHeaderListener l) {

        this.initHeaderListener = l;
    }

    public void changeTitle(String name) {

        this.header_title.setText(name);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.header_btn_left:
                this.initHeaderListener.OnSetLeftOnclick(view);
                backEvent();
                break;

            case R.id.header_btn_right:
                this.initHeaderListener.OnSetRightOnclick(view);
                break;
        }
    }

    public int getSkinRandom() {

        int max = skin.length;
        int min = 0;
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        Log.d(TAG, "random=" + s);
        return s;
    }

    private final int[] skin = {

//            R.drawable.skin_default,


    };

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setBackground(Drawable id) {
        findViewById(R.id.thermostat_bg).setBackground(id);
    }


    /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        Class<?> c = null;
        Object obj = null;
        java.lang.reflect.Field field = null;
        int x = 0;
        int statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = getResources().getDimensionPixelSize(x);
            return statusBarHeight;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusBarHeight;
    }

    /**
     * @param parent_view AS View
     * @param view        pop content
     * @param position    Gravity.TOP or Gravity.BOTTOM
     */
    public void showPopView(View parent_view, View view, int position) {

        if (parent_view == null) {

            parent_view = header_include;
        }
        if (mpopupWindow == null) {
            mpopupWindow = new PopupWindow(this);
            mpopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mpopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            mpopupWindow.setBackgroundDrawable(new BitmapDrawable());

            mpopupWindow.setFocusable(true);
            mpopupWindow.setOutsideTouchable(true);
        }
        mpopupWindow.setContentView(view);
        if (position == Gravity.TOP) {

            int height_parent_view = getResources().getDimensionPixelOffset(R.dimen.header_height);
            mpopupWindow.showAsDropDown(parent_view, 0, -height_parent_view);

        } else {
            mpopupWindow.showAtLocation(parent_view, Gravity.BOTTOM, 0, 0);

        }
        mpopupWindow.update();
    }

    public void dismissPopView() {

        if (mpopupWindow != null) mpopupWindow.dismiss();
    }

    public String getStringXml(int id) {

        if (resources == null) resources = getResources();

        return resources.getString(id);
    }

    public void backEvent() {
        myFinish();
    }

    public void myFinish() {

        this.finish();
    }

}
